﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.EntityFramework.ModelMapper;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.EntityFramework.Implementation.Dictionary
{
    /// <summary>
    /// CustomerDao  class
    /// </summary>
    public class EntityCustomerDao : ICustomerDao
    {
        public List<BusinessObjects.BussinessDictionary.Customer> GetPaged(int unitId, int currentPage)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var result = context.Customers.AsQueryable().Where(x => x.UnitID == unitId).OrderByDescending(x => x.CustomerID).ToList();
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0 && currentPage > 0)
                {
                    var start = (currentPage - 1) * pageSize;
                    result = result.Skip(start).Take(pageSize).ToList();
                }
                return result.Map().ToList();
            }
        }

        public List<BusinessObjects.BussinessDictionary.Customer> GetCustomerByUnit(int unitId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var result = context.Customers.AsQueryable().Where(x => x.UnitID == unitId).OrderByDescending(x => x.CustomerID).ToList();
                return result.Map().ToList();
            }
        }

        public int GetPageSize(int unitId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                var totalRow = context.Customers.Count();
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if(pageSize>0)
                {
                    var round = totalRow % pageSize;
                    if(round > 0)
                    {
                        return totalRow/pageSize + 1;
                    }
                    return totalRow/pageSize;

                }                
                return 1;
            }
        }

        public BusinessObjects.BussinessDictionary.Customer GetCustomer(int customerId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return context.Customers.SingleOrDefault(x => x.CustomerID == customerId).Map();
            }
        }

        public string InsertCustomer(BusinessObjects.BussinessDictionary.Customer customer)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    context.Customers.Add(customer.Map());
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string UpdateCustomer(BusinessObjects.BussinessDictionary.Customer customer)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.Customers.SingleOrDefault(x => x.CustomerID == customer.CustomerId);
                    if (result != null)
                    {
                        result.CustomerID = customer.CustomerId;
                        result.UnitID = customer.UnitId;
                        result.Code = customer.Code;
                        result.Name = customer.Name;
                        result.Address = customer.Address;
                        result.PhoneNumber = customer.PhoneNumber;
                        result.Fax = customer.Fax;
                        result.CompanyName = customer.CompanyName;
                        result.ContactName = customer.ContactName;
                        result.Description = customer.Description;
                        result.IsActive = customer.IsActive;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string DeleteCustomer(BusinessObjects.BussinessDictionary.Customer customer)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.Customers.SingleOrDefault(x => x.CustomerID == customer.CustomerId);
                    if (result != null)
                    {
                        context.Customers.Remove(result);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }
    }
}
